package cn.com.czpb.client.module.news.view;

import android.app.Activity;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import cn.com.czpb.client.R;
import cn.com.czpb.client.custorm.TopPicViewPager;
import cn.com.czpb.client.model.news.NewsFlashBean;
import cn.com.czpb.client.module.news.adapter.TopviewpagerAdapter;
import cn.com.czpb.client.utils.LogUtils;
import me.relex.circleindicator.CircleIndicator;

/**
 * Created by 猪猪侠 on 2017/7/25.
 */

public class TopViewPagerViewMainLive {
    TopPicViewPager testPager;//viewpager
    private CircleIndicator mGuideIndicator;
    private Handler viewHandler = new Handler();
    private MyRun switchTask;//切换线程
    private boolean isContinue = true;//是否可以自动切换
    private TopviewpagerAdapter topAdapter;
    private int interval = 4;//播放间隔时间
    private View rootView;
    private Activity mActivity;
    private TextView tv_title_id;
    private static TopViewPagerViewMainLive instance;
    public static synchronized TopViewPagerViewMainLive getInstance(Activity activity){
        if (instance ==null){
            instance=new TopViewPagerViewMainLive(activity);
        }

        return instance;
    }

    public TopViewPagerViewMainLive(Activity activity){
        //
        this.mActivity = activity;
        initView(activity);
    }

    public View getView(){
        return rootView;
    }

    /**
     * @param context
     */
    private void initView(Activity context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        rootView = layoutInflater.inflate(R.layout.view_news_topviewpager,null);
        testPager= rootView.findViewById(R.id.test_pager);
        tv_title_id = rootView.findViewById(R.id.tv_title_id);



        mGuideIndicator = rootView.findViewById(R.id.indicator);
        topAdapter = new TopviewpagerAdapter(context);
        testPager.setAdapter(topAdapter);
        viewHandler.removeCallbacksAndMessages(null);
        switchTask = new MyRun();
        testPager.setCurrentItem(0);
        testPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == topAdapter.getCount() - 1) {
//                    testPager.setCurrentItem(1, false);
                    testPager.setCurrentItem(topAdapter.getCount() - 1, false);
                    tv_title_id.setText(topAdapter.getBean(position).getTitle());
                    return;
                } else if (0 == position) {
//                    testPager.setCurrentItem(topAdapter.getCount() - 2, false);
                    testPager.setCurrentItem(0, false);
                }
                tv_title_id.setText(topAdapter.getBean(position).getTitle());
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void reFreshFlashData(List<NewsFlashBean> mViewPagelist){
        LogUtils.i("this-->"+this);
        topAdapter.clearData();
        topAdapter.addData(mViewPagelist);
        mGuideIndicator.setViewPager(testPager);
        LogUtils.i("setflashdata-->"+mViewPagelist.size());
        if (mViewPagelist.size() > 1) {
            testPager.setCurrentItem(0, false);
        }
        tv_title_id.setText(topAdapter.getBean(0).getTitle());
        if (topAdapter.getListSize() > 1) {
            if (switchTask.isStart){
                viewHandler.removeCallbacks(switchTask);
            }
            switchTask.run();
        }
    }


    /**
     * 轮播定时滚动
     */
    /**
     * 轮播定时滚动
     */
    class MyRun implements Runnable {
        private boolean isStart = false;

        public void run() {
            try {
                if (isContinue && isStart) {
                    int current = testPager.getCurrentItem();
                    int total = testPager.getAdapter().getCount();
                    if (total == current + 1) {
                        testPager.setCurrentItem(0, false);
                    } else {
                        testPager.setCurrentItem(current + 1, true);
                    }
                } else {
                    isStart = true;
                }
            }catch (Exception e){
                e.printStackTrace();
            }


            viewHandler.postDelayed(switchTask, interval * 1000);
        }
    }

}
